﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Stores.IViews;
using MyCommerce.Presentation.Stores.Presenters;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.Stores.Views
{
    public partial class ProductStockTransfer : BaseForm, IProductStockTransfer
    {

        private ProductStockTransferPresenter vPresenter;

        public ProductStockTransfer()
        {
            InitializeComponent();
            vPresenter = new ProductStockTransferPresenter((IProductStockTransfer)this);
        }

        private void ProductStockTransfer_Load(object sender, EventArgs e)
        {
            OnLoad(sender, e);
        }

        private void tsSaveButtonToolStrip_Click(object sender, EventArgs e)
        {
            try
            {
                if (!DataAutoValidate())
                    return;

                if (OnSave != null)
                {
                    OnSave(this, true);
                    Context.ShowInformationMessageBox("La información se guardó correctamente.", "IMPORTANTE");
                }
                CleanScreen();
            }
            catch (Exception ex)
            {
                Context.HandleException(ex);
            }
            
        }

        private void tsCancelButtonToolStrip_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region Interface

        public List<Innova.Microfinanzas.BusinessEntities.ProductStock> colProductStockOrigen
        {
            set { gridStoreOrigen.DataSource = value; }
        }

        public List<Innova.Microfinanzas.BusinessEntities.ProductStock> colProductStockDestiny
        {
            set { gridStoreDestiny.DataSource = value; }
        }

        public List<Innova.Microfinanzas.BusinessEntities.Product> colProduct
        {
            set {
                cmbProduct.ValueMember = "IdProduct";
                cmbProduct.DisplayMember = "Model";
                cmbProduct.DataSource = value;
            }
        }

        public List<Innova.Microfinanzas.BusinessEntities.Store> colStoreOrigen
        {
            set {
                cmbStoreOrigen.ValueMember = "IdStore";
                cmbStoreOrigen.DisplayMember = "Name";
                cmbStoreOrigen.DataSource = value;
            }
        }

        public List<Innova.Microfinanzas.BusinessEntities.Store> colStoreDestiny
        {
            set
            {
                cmbStoreDestiny.ValueMember = "IdStore";
                cmbStoreDestiny.DisplayMember = "Name";
                cmbStoreDestiny.DataSource = value;
            }
        }

        public Innova.Microfinanzas.BusinessEntities.StockTransfer objStockTransfer
        {
            get 
            {
                StockTransfer newStockTransfer = new StockTransfer();
                newStockTransfer.IdProduct = (long)cmbProduct.Value;
                newStockTransfer.StockQuantity = (int)txtQuantity.Value;
                newStockTransfer.IdStoreOrigen = (long)cmbStoreOrigen.SelectedValue;
                newStockTransfer.IdStoreDestiny = (long)cmbStoreDestiny.SelectedValue;
                return newStockTransfer;
            }
        }

        public new event EventHandler OnLoad;

        public event OnSaveEventHandler OnSave;

        public event Innova.FoundationFramework.DataTypes.GenericEventHandler<long> OnChangeOrigen;

        public event GenericEventHandler<long> OnChangeDestiny;

        #endregion

        private void cmbStoreOrigen_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnChangeOrigen(sender, new GenericEventArgs<long>(Convert.ToInt64(cmbStoreOrigen.SelectedValue)));
        }

        private void cmbStoreDestiny_SelectedIndexChanged(object sender, EventArgs e)
        {
            OnChangeDestiny(sender, new GenericEventArgs<long>(Convert.ToInt64(cmbStoreDestiny.SelectedValue)));
        }

        private Boolean DataAutoValidate()
        {
            bool result = true;
            StringBuilder message = new StringBuilder();

            if (cmbProduct.SelectedRow == null)
            {
                result = false;
                message.AppendLine("Debe seleccionar un producto.");
            }

            if (String.IsNullOrEmpty(txtQuantity.Text.Trim()))
            {
                result = false;
                message.AppendLine("Debe colocar la cantidad del producto a transferir.");
            }

            if (!result)
            {
                Context.ShowErrorMessageBox(message.ToString(), "Validación de datos.");
            }

            return result;
        }

        private void CleanScreen()
        {
            txtQuantity.Text = string.Empty;
        }
    }
}
